package cn.handong.tool.algorithm.BFS;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;
import java.util.Scanner;

public class 迷宫_最短路问题 {
    static String[][] a; // 迷宫
    static Boolean[][] inq; // 是否已来过
    static int n;
    static int m;
    static int[] X = {0,0,1,-1}; //增量数组
    static int[] Y = {1,-1,0,0};
    static Node1 S = new Node1();
    static Node1 T = new Node1();
    // 检测位置（x,y）是否有效
    static Boolean test(int x,int y){
        if (x < 0 || y < 0 || x >= n || y >= m){ //是否越界
            return false;
        }
        if (Objects.equals(a[x][y], "*")){ //墙壁
            return false;
        }
        if (inq[x][y]){ //已入过队
            return false;
        }
        return true;
    }
    static int bfs(){
        Queue<Node1> q = new LinkedList<Node1>();
        q.offer(S);
        while (!q.isEmpty()){
            Node1 top = q.poll();
            if (top.x == T.x && top.y == T.y){
                return top.step;
            }
            Node1 node = new Node1();
            for (int i = 0; i < 4; i++) {
                int newX = top.x + X[i];
                int newY = top.y + Y[i];
                if (test(newX,newY)){
                    node.x = newX;
                    node.y = newY;
                    node.step = top.step ++;
                    q.offer(node);
                    inq[newX][newY] = true;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt(); //
        m = in.nextInt();
        in.nextLine();
        a = new String[n][m];
        inq = new Boolean[n][m];
        for (int i = 0; i < n; i++) {
            String[] str = in.nextLine().split("");
            for (int j = 0; j < m; j++) {
                a[i][j] = str[j];
                inq[i][j] = false;
            }
        }
        S.x = in.nextInt(); //起点
        S.y = in.nextInt();
        T.x = in.nextInt();
        T.y = in.nextInt();
        S.step = 0;
        System.out.println(bfs());
    }
}



class Node1{
    int x,y; //位置（X,Y）
    int step; //从起点S到该点的最短步数
}